home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / Python1.4_Source / Objects / classobject.c < prev    next >
C/C++ Source or Header  |  1998-06-24  |  33KB  |  1,457 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Class object implementation */
  33.  
  34. #include "allobjects.h"
  35. #include "structmember.h"
  36.  
  37. /* Forward */
  38. #include "protos/classobject_protos1.h"
  39. static object *class_lookup PROTO((classobject *, object *, classobject **));
  40. static object *instance_getattr1 PROTO((instanceobject *, object *));
  41.  
  42. object *
  43. newclassobject(bases, dict, name)
  44.     object *bases; /* NULL or tuple of classobjects! */
  45.     object *dict;
  46.     object *name; /* String; NULL if unknown */
  47. {
  48. #ifdef SUPPORT_OBSOLETE_ACCESS
  49.     int pos;
  50.     object *key, *value;
  51. #endif
  52.     classobject *op, *dummy;
  53.     static object *getattrstr, *setattrstr, *delattrstr;
  54.     static object *docstr;
  55.     if (docstr == NULL) {
  56.         docstr= newstringobject("__doc__");
  57.         if (docstr == NULL)
  58.             return NULL;
  59.     }
  60.     if (mappinglookup(dict, docstr) == NULL) {
  61.         if (mappinginsert(dict, docstr, None) < 0)
  62.             return NULL;
  63.     }
  64.     if (bases == NULL) {
  65.         bases = newtupleobject(0);
  66.         if (bases == NULL)
  67.             return NULL;
  68.     }
  69.     else
  70.         INCREF(bases);
  71.     op = NEWOBJ(classobject, &Classtype);
  72.     if (op == NULL) {
  73.         DECREF(bases);
  74.         return NULL;
  75.     }
  76.     op->cl_bases = bases;
  77.     INCREF(dict);
  78.     op->cl_dict = dict;
  79.     XINCREF(name);
  80.     op->cl_name = name;
  81.     if (getattrstr == NULL) {
  82.         getattrstr = newstringobject("__getattr__");
  83.         setattrstr = newstringobject("__setattr__");
  84.         delattrstr = newstringobject("__delattr__");
  85.     }
  86.     op->cl_getattr = class_lookup(op, getattrstr, &dummy);
  87.     op->cl_setattr = class_lookup(op, setattrstr, &dummy);
  88.     op->cl_delattr = class_lookup(op, delattrstr, &dummy);
  89.     XINCREF(op->cl_getattr);
  90.     XINCREF(op->cl_setattr);
  91.     XINCREF(op->cl_delattr);
  92. #ifdef SUPPORT_OBSOLETE_ACCESS
  93.     pos = 0;
  94.     while (mappinggetnext(dict, &pos, &key, &value)) {
  95.         if (is_accessobject(value))
  96.             setaccessowner(value, (object *)op);
  97.     }
  98. #endif
  99.     return (object *) op;
  100. }
  101.  
  102. /* Class methods */
  103.  
  104. static void
  105. class_dealloc(op)
  106.     classobject *op;
  107. {
  108.     DECREF(op->cl_bases);
  109.     DECREF(op->cl_dict);
  110.     XDECREF(op->cl_name);
  111.     free((ANY *)op);
  112. }
  113.  
  114. static object *
  115. class_lookup(cp, name, pclass)
  116.     classobject *cp;
  117.     object *name;
  118.     classobject **pclass;
  119. {
  120.     int i, n;
  121.     object *value = mappinglookup(cp->cl_dict, name);
  122.     if (value != NULL) {
  123.         *pclass = cp;
  124.         return value;
  125.     }
  126.     n = gettuplesize(cp->cl_bases);
  127.     for (i = 0; i < n; i++) {
  128.         object *v = class_lookup((classobject *)
  129.                  gettupleitem(cp->cl_bases, i), name, pclass);
  130.         if (v != NULL)
  131.             return v;
  132.     }
  133.     return NULL;
  134. }
  135.  
  136. static object *
  137. class_getattr(op, name)
  138.     register classobject *op;
  139.     object *name;
  140. {
  141.     register object *v;
  142.     register char *sname = getstringvalue(name);
  143.     classobject *class;
  144.     if (sname[0] == '_' && sname[1] == '_') {
  145.         if (strcmp(sname, "__dict__") == 0) {
  146.             if (getrestricted()) {
  147.                 err_setstr(RuntimeError,
  148.                        "class.__dict__ not accessible in restricted mode");
  149.                 return NULL;
  150.             }
  151.             INCREF(op->cl_dict);
  152.             return op->cl_dict;
  153.         }
  154.         if (strcmp(sname, "__bases__") == 0) {
  155.             INCREF(op->cl_bases);
  156.             return op->cl_bases;
  157.         }
  158.         if (strcmp(sname, "__name__") == 0) {
  159.             if (op->cl_name == NULL)
  160.                 v = None;
  161.             else
  162.                 v = op->cl_name;
  163.             INCREF(v);
  164.             return v;
  165.         }
  166.     }
  167.     v = class_lookup(op, name, &class);
  168.     if (v == NULL) {
  169.         err_setval(AttributeError, name);
  170.         return NULL;
  171.     }
  172. #ifdef SUPPORT_OBSOLETE_ACCESS
  173.     if (is_accessobject(v)) {
  174.         v = getaccessvalue(v, getowner());
  175.         if (v == NULL)
  176.             return NULL;
  177.     }
  178.     else
  179. #endif
  180.         INCREF(v);
  181.     if (is_funcobject(v)) {
  182.         object *w = newinstancemethodobject(v, (object *)NULL,
  183.                             (object *)class);
  184.         DECREF(v);
  185.         v = w;
  186.     }
  187.     return v;
  188. }
  189.  
  190. static int
  191. class_setattr(op, name, v)
  192.     classobject *op;
  193.     object *name;
  194.     object *v;
  195. {
  196. #ifdef SUPPORT_OBSOLETE_ACCESS
  197.     object *ac;
  198. #endif
  199.     char *sname = getstringvalue(name);
  200.     if (sname[0] == '_' && sname[1] == '_') {
  201.         int n = getstringsize(name);
  202.         if (sname[n-1] == '_' && sname[n-2] == '_') {
  203.             err_setstr(TypeError, "read-only special attribute");
  204.             return -1;
  205.         }
  206.     }
  207.     if (getrestricted()) {
  208.         err_setstr(RuntimeError,
  209.                "classes are read-only in restricted mode");
  210.         return -1;
  211.     }
  212. #ifdef SUPPORT_OBSOLETE_ACCESS
  213.     ac = mappinglookup(op->cl_dict, name);
  214.     if (ac != NULL && is_accessobject(ac))
  215.         return setaccessvalue(ac, getowner(), v);
  216. #endif
  217.     if (v == NULL) {
  218.         int rv = mappingremove(op->cl_dict, name);
  219.         if (rv < 0)
  220.             err_setstr(AttributeError,
  221.                    "delete non-existing class attribute");
  222.         return rv;
  223.     }
  224.     else
  225.         return mappinginsert(op->cl_dict, name, v);
  226. }
  227.  
  228. static object *
  229. class_repr(op)
  230.     classobject *op;
  231. {
  232.     char buf[140];
  233.     char *name;
  234.     if (op->cl_name == NULL || !is_stringobject(op->cl_name))
  235.         name = "?";
  236.     else
  237.         name = getstringvalue(op->cl_name);
  238.     sprintf(buf, "<class %.100s at %lx>", name, (long)op);
  239.     return newstringobject(buf);
  240. }
  241.  
  242. typeobject Classtype = {
  243.     OB_HEAD_INIT(&Typetype)
  244.     0,
  245.     "class",
  246.     sizeof(classobject),
  247.     0,
  248.     (destructor)class_dealloc, /*tp_dealloc*/
  249.     0,        /*tp_print*/
  250.     0,        /*tp_getattr*/
  251.     0,        /*tp_setattr*/
  252.     0,        /*tp_compare*/
  253.     (reprfunc)class_repr, /*tp_repr*/
  254.     0,        /*tp_as_number*/
  255.     0,        /*tp_as_sequence*/
  256.     0,        /*tp_as_mapping*/
  257.     0,        /*tp_hash*/
  258.     0,        /*tp_call*/
  259.     0,        /*tp_str*/
  260.     (getattrofunc)class_getattr, /*tp_getattro*/
  261.     (setattrofunc)class_setattr, /*tp_setattro*/
  262. };
  263.  
  264. int
  265. issubclass(class, base)
  266.     object *class;
  267.     object *base;
  268. {
  269.     int i, n;
  270.     classobject *cp;
  271.     if (class == base)
  272.         return 1;
  273.     if (class == NULL || !is_classobject(class))
  274.         return 0;
  275.     cp = (classobject *)class;
  276.     n = gettuplesize(cp->cl_bases);
  277.     for (i = 0; i < n; i++) {
  278.         if (issubclass(gettupleitem(cp->cl_bases, i), base))
  279.             return 1;
  280.     }
  281.     return 0;
  282. }
  283.  
  284.  
  285. /* Instance objects */
  286.  
  287. #ifdef SUPPORT_OBSOLETE_ACCESS
  288. static int
  289. addaccess(class, inst)
  290.     classobject *class;
  291.     instanceobject *inst;
  292. {
  293.     int i, n, pos, ret;
  294.     object *key, *value, *ac;
  295.     
  296.     n = gettuplesize(class->cl_bases);
  297.     for (i = 0; i < n; i++) {
  298.         if (addaccess((classobject *)gettupleitem(class->cl_bases, i), inst) < 0)
  299.             return -1;
  300.     }
  301.     
  302.     pos = 0;
  303.     while (mappinggetnext(class->cl_dict, &pos, &key, &value)) {
  304.         if (!is_accessobject(value))
  305.             continue;
  306.         if (hasaccessvalue(value))
  307.             continue;
  308.         ac = dict2lookup(inst->in_dict, key);
  309.         if (ac != NULL && is_accessobject(ac)) {
  310.             err_setval(ConflictError, key);
  311.             return -1;
  312.         }
  313.         ac = cloneaccessobject(value);
  314.         if (ac == NULL)
  315.             return -1;
  316.         ret = dict2insert(inst->in_dict, key, ac);
  317.         DECREF(ac);
  318.         if (ret != 0)
  319.             return -1;
  320.         }
  321.     return 0;
  322. }
  323. #endif
  324.  
  325. object *
  326. newinstanceobject(class, arg, kw)
  327.     object *class;
  328.     object *arg;
  329.     object *kw;
  330. {
  331.     register instanceobject *inst;
  332.     object *init;
  333.     static object *initstr;
  334.     if (!is_classobject(class)) {
  335.         err_badcall();
  336.         return NULL;
  337.     }
  338.     inst = NEWOBJ(instanceobject, &Instancetype);
  339.     if (inst == NULL)
  340.         return NULL;
  341.     INCREF(class);
  342.     inst->in_class = (classobject *)class;
  343.     inst->in_dict = newdictobject();
  344.     if (inst->in_dict == NULL
  345. #ifdef SUPPORT_OBSOLETE_ACCESS
  346.         || addaccess((classobject *)class, inst) != 0
  347. #endif
  348.         ) {
  349.         DECREF(inst);
  350.         return NULL;
  351.     }
  352.     if (initstr == NULL)
  353.         initstr = newstringobject("__init__");
  354.     init = instance_getattr1(inst, initstr);
  355.     if (init == NULL) {
  356.         err_clear();
  357.         if (arg != NULL && (!is_tupleobject(arg) ||
  358.                     gettuplesize(arg) != 0)
  359.             || kw != NULL && (!is_dictobject(kw) ||
  360.                       getdictsize(kw) != 0)) {
  361.             err_setstr(TypeError,
  362.                    "this constructor takes no arguments");
  363.             DECREF(inst);
  364.             inst = NULL;
  365.         }
  366.     }
  367.     else {
  368.         object *res = PyEval_CallObjectWithKeywords(init, arg, kw);
  369.         DECREF(init);
  370.         if (res == NULL) {
  371.             DECREF(inst);
  372.             inst = NULL;
  373.         }
  374.         else {
  375.             if (res != None) {
  376.                 err_setstr(TypeError,
  377.                        "__init__() should return None");
  378.                 DECREF(inst);
  379.                 inst = NULL;
  380.             }
  381.             DECREF(res);
  382.         }
  383.     }
  384.     return (object *)inst;
  385. }
  386.  
  387. /* Instance methods */
  388.  
  389. static void
  390. instance_dealloc(inst)
  391.     register instanceobject *inst;
  392. {
  393.     object *error_type, *error_value, *error_traceback;
  394.     object *del;
  395.     static object *delstr;
  396.     /* Call the __del__ method if it exists.  First temporarily
  397.        revive the object and save the current exception, if any. */
  398. #ifdef Py_TRACE_REFS
  399.     /* much too complicated if Py_TRACE_REFS defined */
  400.     extern long ref_total;
  401.     inst->ob_type = &Instancetype;
  402.     NEWREF(inst);
  403.     ref_total--;        /* compensate for increment in NEWREF */
  404. #ifdef COUNT_ALLOCS
  405.     inst->ob_type->tp_alloc--; /* ditto */
  406. #endif
  407. #else /* !Py_TRACE_REFS */
  408.     INCREF(inst);
  409. #endif /* !Py_TRACE_REFS */
  410.     err_fetch(&error_type, &error_value, &error_traceback);
  411.     if (delstr == NULL)
  412.         delstr = newstringobject("__del__");
  413.     if ((del = instance_getattr1(inst, delstr)) != NULL) {
  414.         object *res = call_object(del, (object *)NULL);
  415.         DECREF(del);
  416.         if (res == NULL) {
  417.             PyObject *f = sysget("stderr");
  418.             err_clear();
  419.             if (f != NULL) {
  420.                 writestring("exception in ", f);
  421.                 writestring(PyString_AsString(
  422.                     inst->in_class->cl_name), f);
  423.                 writestring(".__del__() ignored\n", f);
  424.             }
  425.         }
  426.         else
  427.             DECREF(res);
  428.     }
  429.     /* Restore the saved exception and undo the temporary revival */
  430.     err_restore(error_type, error_value, error_traceback);
  431.     /* Can't use DECREF here, it would cause a recursive call */
  432.     if (--inst->ob_refcnt > 0) {
  433. #ifdef COUNT_ALLOCS
  434.         inst->ob_type->tp_free--;
  435. #endif
  436.         return; /* __del__ added a reference; don't delete now */
  437.     }
  438. #ifdef Py_TRACE_REFS
  439. #ifdef COUNT_ALLOCS
  440.     inst->ob_type->tp_free--;    /* compensate for increment in UNREF */
  441. #endif
  442.     UNREF(inst);
  443.     inst->ob_type = NULL;
  444. #endif /* Py_TRACE_REFS */
  445.     DECREF(inst->in_class);
  446.     XDECREF(inst->in_dict);
  447.     free((ANY *)inst);
  448. }
  449.  
  450. static object *
  451. instance_getattr1(inst, name)
  452.     register instanceobject *inst;
  453.     object *name;
  454. {
  455.     register object *v;
  456.     register char *sname = getstringvalue(name);
  457.     classobject *class;
  458.     if (sname[0] == '_' && sname[1] == '_') {
  459.         if (strcmp(sname, "__dict__") == 0) {
  460.             if (getrestricted()) {
  461.                 err_setstr(RuntimeError,
  462.                        "instance.__dict__ not accessible in restricted mode");
  463.                 return NULL;
  464.             }
  465.             INCREF(inst->in_dict);
  466.             return inst->in_dict;
  467.         }
  468.         if (strcmp(sname, "__class__") == 0) {
  469.             INCREF(inst->in_class);
  470.             return (object *)inst->in_class;
  471.         }
  472.     }
  473.     class = NULL;
  474.     v = mappinglookup(inst->in_dict, name);
  475.     if (v == NULL) {
  476.         v = class_lookup(inst->in_class, name, &class);
  477.         if (v == NULL) {
  478.             err_setval(AttributeError, name);
  479.             return NULL;
  480.         }
  481.     }
  482. #ifdef SUPPORT_OBSOLETE_ACCESS
  483.     if (is_accessobject(v)) {
  484.         v = getaccessvalue(v, getowner());
  485.         if (v == NULL)
  486.             return NULL;
  487.     }
  488.     else
  489. #endif
  490.         INCREF(v);
  491.     if (class != NULL) {
  492.         if (is_funcobject(v)) {
  493.             object *w = newinstancemethodobject(v, (object *)inst,
  494.                                 (object *)class);
  495.             DECREF(v);
  496.             v = w;
  497.         }
  498.         else if (is_instancemethodobject(v)) {
  499.             object *im_class = instancemethodgetclass(v);
  500.             /* Only if classes are compatible */
  501.             if (issubclass((object *)class, im_class)) {
  502.                 object *im_func = instancemethodgetfunc(v);
  503.                 object *w = newinstancemethodobject(im_func,
  504.                         (object *)inst, im_class);
  505.                 DECREF(v);
  506.                 v = w;
  507.             }
  508.         }
  509.     }
  510.     return v;
  511. }
  512.  
  513. static object *
  514. instance_getattr(inst, name)
  515.     register instanceobject *inst;
  516.     object *name;
  517. {
  518.     register object *func, *res;
  519.     res = instance_getattr1(inst, name);
  520.     if (res == NULL && (func = inst->in_class->cl_getattr) != NULL) {
  521.         object *args;
  522.         err_clear();
  523.         args = mkvalue("(OO)", inst, name);
  524.         if (args == NULL)
  525.             return NULL;
  526.         res = call_object(func, args);
  527.         DECREF(args);
  528.     }
  529.     return res;
  530. }
  531.  
  532. static int
  533. instance_setattr1(inst, name, v)
  534.     instanceobject *inst;
  535.     object *name;
  536.     object *v;
  537. {
  538. #ifdef SUPPORT_OBSOLETE_ACCESS
  539.     object *ac;
  540.     ac = mappinglookup(inst->in_dict, name);
  541.     if (ac != NULL && is_accessobject(ac))
  542.         return setaccessvalue(ac, getowner(), v);
  543. #endif
  544.     if (v == NULL) {
  545.         int rv = mappingremove(inst->in_dict, name);
  546.         if (rv < 0)
  547.             err_setstr(AttributeError,
  548.                    "delete non-existing instance attribute");
  549.         return rv;
  550.     }
  551.     else
  552.         return mappinginsert(inst->in_dict, name, v);
  553. }
  554.  
  555. static int
  556. instance_setattr(inst, name, v)
  557.     instanceobject *inst;
  558.     object *name;
  559.     object *v;
  560. {
  561.     object *func, *args, *res;
  562.     char *sname = getstringvalue(name);
  563.     if (sname[0] == '_' && sname[1] == '_'
  564.         && (strcmp(sname, "__dict__") == 0 ||
  565.         strcmp(sname, "__class__") == 0)) {
  566.             int n = getstringsize(name);
  567.         if (sname[n-1] == '_' && sname[n-2] == '_') {
  568.             err_setstr(TypeError, "read-only special attribute");
  569.             return -1;
  570.         }
  571.     }
  572.     if (v == NULL)
  573.         func = inst->in_class->cl_delattr;
  574.     else
  575.         func = inst->in_class->cl_setattr;
  576.     if (func == NULL)
  577.         return instance_setattr1(inst, name, v);
  578.     if (v == NULL)
  579.         args = mkvalue("(OO)", inst, name);
  580.     else
  581.         args = mkvalue("(OOO)", inst, name, v);
  582.     if (args == NULL)
  583.         return -1;
  584.     res = call_object(func, args);
  585.     DECREF(args);
  586.     if (res == NULL)
  587.         return -1;
  588.     DECREF(res);
  589.     return 0;
  590. }
  591.  
  592. static object *
  593. instance_repr(inst)
  594.     instanceobject *inst;
  595. {
  596.     object *func;
  597.     object *res;
  598.     static object *reprstr;
  599.  
  600.     if (reprstr == NULL)
  601.         reprstr = newstringobject("__repr__");
  602.     func = instance_getattr(inst, reprstr);
  603.     if (func == NULL) {
  604.         char buf[140];
  605.         object *classname = inst->in_class->cl_name;
  606.         char *cname;
  607.         if (classname != NULL && is_stringobject(classname))
  608.             cname = getstringvalue(classname);
  609.         else
  610.             cname = "?";
  611.         err_clear();
  612.         sprintf(buf, "<%.100s instance at %lx>", cname, (long)inst);
  613.         return newstringobject(buf);
  614.     }
  615.     res = call_object(func, (object *)NULL);
  616.     DECREF(func);
  617.     return res;
  618. }
  619.  
  620. static object *
  621. instance_compare1(inst, other)
  622.     object *inst, *other;
  623. {
  624.     return instancebinop(inst, other, "__cmp__", "__rcmp__",
  625.                  instance_compare1);
  626. }
  627.  
  628. static int
  629. instance_compare(inst, other)
  630.     object *inst, *other;
  631. {
  632.     object *result;
  633.     long outcome;
  634.     result = instance_compare1(inst, other);
  635.     if (result == NULL || !is_intobject(result)) {
  636.     error:
  637.         err_clear();
  638.         return (inst < other) ? -1 : 1;
  639.     }
  640.     outcome = getintvalue(result);
  641.     DECREF(result);
  642.     if (outcome < 0)
  643.         return -1;
  644.     else if (outcome > 0)
  645.         return 1;
  646.     return 0;
  647. }
  648.  
  649. static long
  650. instance_hash(inst)
  651.     instanceobject *inst;
  652. {
  653.     object *func;
  654.     object *res;
  655.     long outcome;
  656.     static object *hashstr, *cmpstr;
  657.  
  658.     if (hashstr == NULL)
  659.         hashstr = newstringobject("__hash__");
  660.     func = instance_getattr(inst, hashstr);
  661.     if (func == NULL) {
  662.         /* If there is no __cmp__ method, we hash on the address.
  663.            If a __cmp__ method exists, there must be a __hash__. */
  664.         err_clear();
  665.         if (cmpstr == NULL)
  666.             cmpstr = newstringobject("__cmp__");
  667.         func = instance_getattr(inst, cmpstr);
  668.         if (func == NULL) {
  669.             err_clear();
  670.             outcome = (long)inst;
  671.             if (outcome == -1)
  672.                 outcome = -2;
  673.             return outcome;
  674.         }
  675.         err_setstr(TypeError, "unhashable instance");
  676.         return -1;
  677.     }
  678.     res = call_object(func, (object *)NULL);
  679.     DECREF(func);
  680.     if (res == NULL)
  681.         return -1;
  682.     if (is_intobject(res)) {
  683.         outcome = getintvalue(res);
  684.         if (outcome == -1)
  685.             outcome = -2;
  686.     }
  687.     else {
  688.         err_setstr(TypeError, "__hash__() should return an int");
  689.         outcome = -1;
  690.     }
  691.     DECREF(res);
  692.     return outcome;
  693. }
  694.  
  695. static object *getitemstr, *setitemstr, *delitemstr, *lenstr;
  696.  
  697. static int
  698. instance_length(inst)
  699.     instanceobject *inst;
  700. {
  701.     object *func;
  702.     object *res;
  703.     int outcome;
  704.  
  705.     if (lenstr == NULL)
  706.         lenstr = newstringobject("__len__");
  707.     func = instance_getattr(inst, lenstr);
  708.     if (func == NULL)
  709.         return -1;
  710.     res = call_object(func, (object *)NULL);
  711.     DECREF(func);
  712.     if (res == NULL)
  713.         return -1;
  714.     if (is_intobject(res)) {
  715.         outcome = getintvalue(res);
  716.         if (outcome < 0)
  717.             err_setstr(ValueError, "__len__() should return >= 0");
  718.     }
  719.     else {
  720.         err_setstr(TypeError, "__len__() should return an int");
  721.         outcome = -1;
  722.     }
  723.     DECREF(res);
  724.     return outcome;
  725. }
  726.  
  727. static object *
  728. instance_subscript(inst, key)
  729.     instanceobject *inst;
  730.     object *key;
  731. {
  732.     object *func;
  733.     object *arg;
  734.     object *res;
  735.  
  736.     if (getitemstr == NULL)
  737.         getitemstr = newstringobject("__getitem__");
  738.     func = instance_getattr(inst, getitemstr);
  739.     if (func == NULL)
  740.         return NULL;
  741.     arg = mkvalue("(O)", key);
  742.     if (arg == NULL) {
  743.         DECREF(func);
  744.         return NULL;
  745.     }
  746.     res = call_object(func, arg);
  747.     DECREF(func);
  748.     DECREF(arg);
  749.     return res;
  750. }
  751.  
  752. static int
  753. instance_ass_subscript(inst, key, value)
  754.     instanceobject*inst;
  755.     object *key;
  756.     object *value;
  757. {
  758.     object *func;
  759.     object *arg;
  760.     object *res;
  761.  
  762.     if (value == NULL) {
  763.         if (delitemstr == NULL)
  764.             delitemstr = newstringobject("__delitem__");
  765.         func = instance_getattr(inst, delitemstr);
  766.     }
  767.     else {
  768.         if (setitemstr == NULL)
  769.             setitemstr = newstringobject("__setitem__");
  770.         func = instance_getattr(inst, setitemstr);
  771.     }
  772.     if (func == NULL)
  773.         return -1;
  774.     if (value == NULL)
  775.         arg = mkvalue("(O)", key);
  776.     else
  777.         arg = mkvalue("(OO)", key, value);
  778.     if (arg == NULL) {
  779.         DECREF(func);
  780.         return -1;
  781.     }
  782.     res = call_object(func, arg);
  783.     DECREF(func);
  784.     DECREF(arg);
  785.     if (res == NULL)
  786.         return -1;
  787.     DECREF(res);
  788.     return 0;
  789. }
  790.  
  791. static mapping_methods instance_as_mapping = {
  792.     (inquiry)instance_length, /*mp_length*/
  793.     (binaryfunc)instance_subscript, /*mp_subscript*/
  794.     (objobjargproc)instance_ass_subscript, /*mp_ass_subscript*/
  795. };
  796.  
  797. static object *
  798. instance_item(inst, i)
  799.     instanceobject *inst;
  800.     int i;
  801. {
  802.     object *func, *arg, *res;
  803.  
  804.     if (getitemstr == NULL)
  805.         getitemstr = newstringobject("__getitem__");
  806.     func = instance_getattr(inst, getitemstr);
  807.     if (func == NULL)
  808.         return NULL;
  809.     arg = mkvalue("(i)", i);
  810.     if (arg == NULL) {
  811.         DECREF(func);
  812.         return NULL;
  813.     }
  814.     res = call_object(func, arg);
  815.     DECREF(func);
  816.     DECREF(arg);
  817.     return res;
  818. }
  819.  
  820. static object *
  821. instance_slice(inst, i, j)
  822.     instanceobject *inst;
  823.     int i, j;
  824. {
  825.     object *func, *arg, *res;
  826.     static object *getslicestr;
  827.  
  828.     if (getslicestr == NULL)
  829.         getslicestr = newstringobject("__getslice__");
  830.     func = instance_getattr(inst, getslicestr);
  831.     if (func == NULL)
  832.         return NULL;
  833.     arg = mkvalue("(ii)", i, j);
  834.     if (arg == NULL) {
  835.         DECREF(func);
  836.         return NULL;
  837.     }
  838.     res = call_object(func, arg);
  839.     DECREF(func);
  840.     DECREF(arg);
  841.     return res;
  842. }
  843.  
  844. static int
  845. instance_ass_item(inst, i, item)
  846.     instanceobject *inst;
  847.     int i;
  848.     object *item;
  849. {
  850.     object *func, *arg, *res;
  851.  
  852.     if (item == NULL) {
  853.         if (delitemstr == NULL)
  854.             delitemstr = newstringobject("__delitem__");
  855.         func = instance_getattr(inst, delitemstr);
  856.     }
  857.     else {
  858.         if (setitemstr == NULL)
  859.             setitemstr = newstringobject("__setitem__");
  860.         func = instance_getattr(inst, setitemstr);
  861.     }
  862.     if (func == NULL)
  863.         return -1;
  864.     if (item == NULL)
  865.         arg = mkvalue("i", i);
  866.     else
  867.         arg = mkvalue("(iO)", i, item);
  868.     if (arg == NULL) {
  869.         DECREF(func);
  870.         return -1;
  871.     }
  872.     res = call_object(func, arg);
  873.     DECREF(func);
  874.     DECREF(arg);
  875.     if (res == NULL)
  876.         return -1;
  877.     DECREF(res);
  878.     return 0;
  879. }
  880.  
  881. static int
  882. instance_ass_slice(inst, i, j, value)
  883.     instanceobject *inst;
  884.     int i, j;
  885.     object *value;
  886. {
  887.     object *func, *arg, *res;
  888.     static object *setslicestr, *delslicestr;
  889.  
  890.     if (value == NULL) {
  891.         if (delslicestr == NULL)
  892.             delslicestr = newstringobject("__delslice__");
  893.         func = instance_getattr(inst, delslicestr);
  894.     }
  895.     else {
  896.         if (setslicestr == NULL)
  897.             setslicestr = newstringobject("__setslice__");
  898.         func = instance_getattr(inst, setslicestr);
  899.     }
  900.     if (func == NULL)
  901.         return -1;
  902.     if (value == NULL)
  903.         arg = mkvalue("(ii)", i, j);
  904.     else
  905.         arg = mkvalue("(iiO)", i, j, value);
  906.     if (arg == NULL) {
  907.         DECREF(func);
  908.         return -1;
  909.     }
  910.     res = call_object(func, arg);
  911.     DECREF(func);
  912.     DECREF(arg);
  913.     if (res == NULL)
  914.         return -1;
  915.     DECREF(res);
  916.     return 0;
  917. }
  918.  
  919. static sequence_methods instance_as_sequence = {
  920.     (inquiry)instance_length, /*sq_length*/
  921.     0, /*sq_concat*/
  922.     0, /*sq_repeat*/
  923.     (intargfunc)instance_item, /*sq_item*/
  924.     (intintargfunc)instance_slice, /*sq_slice*/
  925.     (intobjargproc)instance_ass_item, /*sq_ass_item*/
  926.     (intintobjargproc)instance_ass_slice, /*sq_ass_slice*/
  927. };
  928.  
  929. static object *
  930. generic_unary_op(self, methodname)
  931.     instanceobject *self;
  932.     object *methodname;
  933. {
  934.     object *func, *res;
  935.  
  936.     if ((func = instance_getattr(self, methodname)) == NULL)
  937.         return NULL;
  938.     res = call_object(func, (object *)NULL);
  939.     DECREF(func);
  940.     return res;
  941. }
  942.  
  943.  
  944. /* Forward */
  945. static int halfbinop Py_PROTO((object *, object *, char *, object **,
  946.               object * (*) Py_PROTO((object *, object *)), int ));
  947.  
  948.  
  949. /* Implement a binary operator involving at least one class instance. */
  950.  
  951. object *
  952. instancebinop(v, w, opname, ropname, thisfunc)
  953.     object *v;
  954.     object *w;
  955.     char *opname;
  956.     char *ropname;
  957.     object * (*thisfunc) PROTO((object *, object *));
  958. {
  959.     char buf[256];
  960.     object *result = NULL;
  961.     if (halfbinop(v, w, opname, &result, thisfunc, 0) <= 0)
  962.         return result;
  963.     if (halfbinop(w, v, ropname, &result, thisfunc, 1) <= 0)
  964.         return result;
  965.     sprintf(buf, "%s nor %s defined for these operands", opname, ropname);
  966.     err_setstr(TypeError, buf);
  967.     return NULL;
  968. }
  969.  
  970.  
  971. /* Try one half of a binary operator involving a class instance.
  972.    Return value:
  973.    -1 if an exception is to be reported right away
  974.    0  if we have a valid result
  975.    1  if we could try another operation
  976. */
  977.  
  978. static object *coerce_obj;
  979.  
  980. static int
  981. halfbinop(v, w, opname, r_result, thisfunc, swapped)
  982.     object *v;
  983.     object *w;
  984.     char *opname;
  985.     object **r_result;
  986.     object * (*thisfunc) PROTO((object *, object *));
  987.     int swapped;
  988. {
  989.     object *func;
  990.     object *args;
  991.     object *coerce;
  992.     object *coerced = NULL;
  993.     object *v1;
  994.     
  995.     if (!is_instanceobject(v))
  996.         return 1;
  997.     if (coerce_obj == NULL) {
  998.         coerce_obj = newstringobject("__coerce__");
  999.         if (coerce_obj == NULL)
  1000.             return -1;
  1001.     }
  1002.     coerce = getattro(v, coerce_obj);
  1003.     if (coerce == NULL) {
  1004.         err_clear();
  1005.     }
  1006.     else {
  1007.         args = mkvalue("(O)", w);
  1008.         if (args == NULL) {
  1009.             return -1;
  1010.         }
  1011.         coerced = call_object(coerce, args);
  1012.         DECREF(args);
  1013.         DECREF(coerce);
  1014.         if (coerced == NULL) {
  1015.             return -1;
  1016.         }
  1017.         if (coerced == None) {
  1018.             DECREF(coerced);
  1019.             return 1;
  1020.         }
  1021.         if (!is_tupleobject(coerced) || gettuplesize(coerced) != 2) {
  1022.             DECREF(coerced);
  1023.             err_setstr(TypeError,
  1024.                    "coercion should return None or 2-tuple");
  1025.             return -1;
  1026.         }
  1027.         v1 = gettupleitem(coerced, 0);
  1028.         w = gettupleitem(coerced, 1);
  1029.         if (v1 != v) {
  1030.             v = v1;
  1031.             if (!is_instanceobject(v) && !is_instanceobject(w)) {
  1032.                 if (swapped)
  1033.                     *r_result = (*thisfunc)(w, v);
  1034.                 else
  1035.                     *r_result = (*thisfunc)(v, w);
  1036.                 DECREF(coerced);
  1037.                 return *r_result == NULL ? -1 : 0;
  1038.             }
  1039.         }
  1040.         w = gettupleitem(coerced, 1);
  1041.     }
  1042.     func = getattr(v, opname);
  1043.     if (func == NULL) {
  1044.         XDECREF(coerced);
  1045.         if (err_occurred() != AttributeError)
  1046.             return -1;
  1047.         err_clear();
  1048.         return 1;
  1049.     }
  1050.     args = mkvalue("(O)", w);
  1051.     if (args == NULL) {
  1052.         DECREF(func);
  1053.         XDECREF(coerced);
  1054.         return -1;
  1055.     }
  1056.     *r_result = call_object(func, args);
  1057.     DECREF(args);
  1058.     DECREF(func);
  1059.     XDECREF(coerced);
  1060.     return *r_result == NULL ? -1 : 0;
  1061. }
  1062.  
  1063. static int
  1064. instance_coerce(pv, pw)
  1065.     object **pv;
  1066.     object **pw;
  1067. {
  1068.     object *v = *pv;
  1069.     object *w = *pw;
  1070.     object *coerce;
  1071.     object *args;
  1072.     object *coerced;
  1073.  
  1074.     if (coerce_obj == NULL) {
  1075.         coerce_obj = newstringobject("__coerce__");
  1076.         if (coerce_obj == NULL)
  1077.             return -1;
  1078.     }
  1079.     coerce = getattro(v, coerce_obj);
  1080.     if (coerce == NULL) {
  1081.         /* No __coerce__ method: always OK */
  1082.         err_clear();
  1083.         INCREF(v);
  1084.         INCREF(w);
  1085.         return 0;
  1086.     }
  1087.     /* Has __coerce__ method: call it */
  1088.     args = mkvalue("(O)", w);
  1089.     if (args == NULL) {
  1090.         return -1;
  1091.     }
  1092.     coerced = call_object(coerce, args);
  1093.     DECREF(args);
  1094.     DECREF(coerce);
  1095.     if (coerced == NULL) {
  1096.         /* __coerce__ call raised an exception */
  1097.         return -1;
  1098.     }
  1099.     if (coerced == None) {
  1100.         /* __coerce__ says "I can't do it" */
  1101.         DECREF(coerced);
  1102.         return 1;
  1103.     }
  1104.     if (!is_tupleobject(coerced) || gettuplesize(coerced) != 2) {
  1105.         /* __coerce__ return value is malformed */
  1106.         DECREF(coerced);
  1107.         err_setstr(TypeError,
  1108.                "coercion should return None or 2-tuple");
  1109.         return -1;
  1110.     }
  1111.     /* __coerce__ returned two new values */
  1112.     *pv = gettupleitem(coerced, 0);
  1113.     *pw = gettupleitem(coerced, 1);
  1114.     INCREF(*pv);
  1115.     INCREF(*pw);
  1116.     DECREF(coerced);
  1117.     return 0;
  1118. }
  1119.  
  1120.  
  1121. #define UNARY(funcname, methodname) \
  1122. static object *funcname(self) instanceobject *self; { \
  1123.     static object *o; \
  1124.     if (o == NULL) o = newstringobject(methodname); \
  1125.     return generic_unary_op(self, o); \
  1126. }
  1127.  
  1128. UNARY(instance_neg, "__neg__")
  1129. UNARY(instance_pos, "__pos__")
  1130. UNARY(instance_abs, "__abs__")
  1131.  
  1132. static int
  1133. instance_nonzero(self)
  1134.     instanceobject *self;
  1135. {
  1136.     object *func, *res;
  1137.     long outcome;
  1138.     static object *nonzerostr;
  1139.  
  1140.     if (nonzerostr == NULL)
  1141.         nonzerostr = newstringobject("__nonzero__");
  1142.     if ((func = instance_getattr(self, nonzerostr)) == NULL) {
  1143.         err_clear();
  1144.         if (lenstr == NULL)
  1145.             lenstr = newstringobject("__len__");
  1146.         if ((func = instance_getattr(self, lenstr)) == NULL) {
  1147.             err_clear();
  1148.             /* Fall back to the default behavior:
  1149.                all instances are nonzero */
  1150.             return 1;
  1151.         }
  1152.     }
  1153.     res = call_object(func, (object *)NULL);
  1154.     DECREF(func);
  1155.     if (res == NULL)
  1156.         return -1;
  1157.     if (!is_intobject(res)) {
  1158.         DECREF(res);
  1159.         err_setstr(TypeError, "__nonzero__ should return an int");
  1160.         return -1;
  1161.     }
  1162.     outcome = getintvalue(res);
  1163.     DECREF(res);
  1164.     if (outcome < 0) {
  1165.         err_setstr(ValueError, "__nonzero__ should return >= 0");
  1166.         return -1;
  1167.     }
  1168.     return outcome > 0;
  1169. }
  1170.  
  1171. UNARY(instance_invert, "__invert__")
  1172. UNARY(instance_int, "__int__")
  1173. UNARY(instance_long, "__long__")
  1174. UNARY(instance_float, "__float__")
  1175. UNARY(instance_oct, "__oct__")
  1176. UNARY(instance_hex, "__hex__")
  1177.  
  1178. /* This version is for ternary calls only (z != None) */
  1179. static object *
  1180. instance_pow(v, w, z)
  1181.     object *v;
  1182.     object *w;
  1183.     object *z;
  1184. {
  1185.     /* XXX Doesn't do coercions... */
  1186.     object *func;
  1187.     object *args;
  1188.     object *result;
  1189.     static object *powstr;
  1190.  
  1191.     if (powstr == NULL)
  1192.         powstr = newstringobject("__pow__");
  1193.     func = getattro(v, powstr);
  1194.     if (func == NULL)
  1195.         return NULL;
  1196.     args = mkvalue("(OO)", w, z);
  1197.     if (args == NULL) {
  1198.         DECREF(func);
  1199.         return NULL;
  1200.     }
  1201.     result = call_object(func, args);
  1202.     DECREF(func);
  1203.     DECREF(args);
  1204.     return result;
  1205. }
  1206.  
  1207. static number_methods instance_as_number = {
  1208.     0, /*nb_add*/
  1209.     0, /*nb_subtract*/
  1210.     0, /*nb_multiply*/
  1211.     0, /*nb_divide*/
  1212.     0, /*nb_remainder*/
  1213.     0, /*nb_divmod*/
  1214.     (ternaryfunc)instance_pow, /*nb_power*/
  1215.     (unaryfunc)instance_neg, /*nb_negative*/
  1216.     (unaryfunc)instance_pos, /*nb_positive*/
  1217.     (unaryfunc)instance_abs, /*nb_absolute*/
  1218.     (inquiry)instance_nonzero, /*nb_nonzero*/
  1219.     (unaryfunc)instance_invert, /*nb_invert*/
  1220.     0, /*nb_lshift*/
  1221.     0, /*nb_rshift*/
  1222.     0, /*nb_and*/
  1223.     0, /*nb_xor*/
  1224.     0, /*nb_or*/
  1225.     (coercion)instance_coerce, /*nb_coerce*/
  1226.     (unaryfunc)instance_int, /*nb_int*/
  1227.     (unaryfunc)instance_long, /*nb_long*/
  1228.     (unaryfunc)instance_float, /*nb_float*/
  1229.     (unaryfunc)instance_oct, /*nb_oct*/
  1230.     (unaryfunc)instance_hex, /*nb_hex*/
  1231. };
  1232.  
  1233. typeobject Instancetype = {
  1234.     OB_HEAD_INIT(&Typetype)
  1235.     0,
  1236.     "instance",
  1237.     sizeof(instanceobject),
  1238.     0,
  1239.     (destructor)instance_dealloc, /*tp_dealloc*/
  1240.     0,            /*tp_print*/
  1241.     0,            /*tp_getattr*/
  1242.     0,            /*tp_setattr*/
  1243.     instance_compare,    /*tp_compare*/
  1244.     (reprfunc)instance_repr, /*tp_repr*/
  1245.     &instance_as_number,    /*tp_as_number*/
  1246.     &instance_as_sequence,    /*tp_as_sequence*/
  1247.     &instance_as_mapping,    /*tp_as_mapping*/
  1248.     (hashfunc)instance_hash, /*tp_hash*/
  1249.     0,            /*tp_call*/
  1250.     0,            /*tp_str*/
  1251.     (getattrofunc)instance_getattr, /*tp_getattro*/
  1252.     (setattrofunc)instance_setattr, /*tp_setattro*/
  1253. };
  1254.  
  1255.  
  1256. /* Instance method objects are used for two purposes:
  1257.    (a) as bound instance methods (returned by instancename.methodname)
  1258.    (b) as unbound methods (returned by ClassName.methodname)
  1259.    In case (b), im_self is NULL
  1260. */
  1261.  
  1262. typedef struct {
  1263.     OB_HEAD
  1264.     object    *im_func;    /* The function implementing the method */
  1265.     object    *im_self;    /* The instance it is bound to, or NULL */
  1266.     object    *im_class;    /* The class that defined the method */
  1267. } instancemethodobject;
  1268.  
  1269. #include "protos/classobject_protos2.h"
  1270.  
  1271. object *
  1272. newinstancemethodobject(func, self, class)
  1273.     object *func;
  1274.     object *self;
  1275.     object *class;
  1276. {
  1277.     register instancemethodobject *im;
  1278.     if (!is_funcobject(func)) {
  1279.         err_badcall();
  1280.         return NULL;
  1281.     }
  1282.     im = NEWOBJ(instancemethodobject, &Instancemethodtype);
  1283.     if (im == NULL)
  1284.         return NULL;
  1285.     INCREF(func);
  1286.     im->im_func = func;
  1287.     XINCREF(self);
  1288.     im->im_self = self;
  1289.     INCREF(class);
  1290.     im->im_class = class;
  1291.     return (object *)im;
  1292. }
  1293.  
  1294. object *
  1295. instancemethodgetfunc(im)
  1296.     register object *im;
  1297. {
  1298.     if (!is_instancemethodobject(im)) {
  1299.         err_badcall();
  1300.         return NULL;
  1301.     }
  1302.     return ((instancemethodobject *)im)->im_func;
  1303. }
  1304.  
  1305. object *
  1306. instancemethodgetself(im)
  1307.     register object *im;
  1308. {
  1309.     if (!is_instancemethodobject(im)) {
  1310.         err_badcall();
  1311.         return NULL;
  1312.     }
  1313.     return ((instancemethodobject *)im)->im_self;
  1314. }
  1315.  
  1316. object *
  1317. instancemethodgetclass(im)
  1318.     register object *im;
  1319. {
  1320.     if (!is_instancemethodobject(im)) {
  1321.         err_badcall();
  1322.         return NULL;
  1323.     }
  1324.     return ((instancemethodobject *)im)->im_class;
  1325. }
  1326.  
  1327. /* Class method methods */
  1328.  
  1329. #define OFF(x) offsetof(instancemethodobject, x)
  1330.  
  1331. static struct memberlist instancemethod_memberlist[] = {
  1332.     {"im_func",    T_OBJECT,    OFF(im_func)},
  1333.     {"im_self",    T_OBJECT,    OFF(im_self)},
  1334.     {"im_class",    T_OBJECT,    OFF(im_class)},
  1335.     /* Dummies that are not handled by getattr() except for __members__ */
  1336.     {"__doc__",    T_INT,        0},
  1337.     {"__name__",    T_INT,        0},
  1338.     {NULL}    /* Sentinel */
  1339. };
  1340.  
  1341. static object *
  1342. instancemethod_getattr(im, name)
  1343.     register instancemethodobject *im;
  1344.     object *name;
  1345. {
  1346.     char *sname = getstringvalue(name);
  1347.     if (sname[0] == '_') {
  1348.         funcobject *func = (funcobject *)(im->im_func);
  1349.         if (strcmp(sname, "__name__") == 0) {
  1350.             INCREF(func->func_name);
  1351.             return func->func_name;
  1352.         }
  1353.         if (strcmp(sname, "__doc__") == 0) {
  1354.             INCREF(func->func_doc);
  1355.             return func->func_doc;
  1356.         }
  1357.     }
  1358.     if (getrestricted()) {
  1359.         err_setstr(RuntimeError,
  1360.                "instance-method attributes not accessible in restricted mode");
  1361.         return NULL;
  1362.     }
  1363.     return getmember((char *)im, instancemethod_memberlist, sname);
  1364. }
  1365.  
  1366. static void
  1367. instancemethod_dealloc(im)
  1368.     register instancemethodobject *im;
  1369. {
  1370.     DECREF(im->im_func);
  1371.     XDECREF(im->im_self);
  1372.     DECREF(im->im_class);
  1373.     free((ANY *)im);
  1374. }
  1375.  
  1376. static int
  1377. instancemethod_compare(a, b)
  1378.     instancemethodobject *a, *b;
  1379. {
  1380.     if (a->im_self != b->im_self)
  1381.         return (a->im_self < b->im_self) ? -1 : 1;
  1382.     return cmpobject(a->im_func, b->im_func);
  1383. }
  1384.  
  1385. static object *
  1386. instancemethod_repr(a)
  1387.     instancemethodobject *a;
  1388. {
  1389.     char buf[240];
  1390.     instanceobject *self = (instanceobject *)(a->im_self);
  1391.     funcobject *func = (funcobject *)(a->im_func);
  1392.     classobject *class = (classobject *)(a->im_class);
  1393.     object *fclassname, *iclassname, *funcname;
  1394.     char *fcname, *icname, *fname;
  1395.     fclassname = class->cl_name;
  1396.     funcname = func->func_name;
  1397.     if (fclassname != NULL && is_stringobject(fclassname))
  1398.         fcname = getstringvalue(fclassname);
  1399.     else
  1400.         fcname = "?";
  1401.     if (funcname != NULL && is_stringobject(funcname))
  1402.         fname = getstringvalue(funcname);
  1403.     else
  1404.         fname = "?";
  1405.     if (self == NULL)
  1406.         sprintf(buf, "<unbound method %.100s.%.100s>", fcname, fname);
  1407.     else {
  1408.         iclassname = self->in_class->cl_name;
  1409.         if (iclassname != NULL && is_stringobject(iclassname))
  1410.             icname = getstringvalue(iclassname);
  1411.         else
  1412.             icname = "?";
  1413.         sprintf(buf, "<method %.60s.%.60s of %.60s instance at %lx>",
  1414.             fcname, fname, icname, (long)self);
  1415.     }
  1416.     return newstringobject(buf);
  1417. }
  1418.  
  1419. static long
  1420. instancemethod_hash(a)
  1421.     instancemethodobject *a;
  1422. {
  1423.     long x, y;
  1424.     if (a->im_self == NULL)
  1425.         x = hashobject(None);
  1426.     else
  1427.         x = hashobject(a->im_self);
  1428.     if (x == -1)
  1429.         return -1;
  1430.     y = hashobject(a->im_func);
  1431.     if (y == -1)
  1432.         return -1;
  1433.     return x ^ y;
  1434. }
  1435.  
  1436. typeobject Instancemethodtype = {
  1437.     OB_HEAD_INIT(&Typetype)
  1438.     0,
  1439.     "instance method",
  1440.     sizeof(instancemethodobject),
  1441.     0,
  1442.     (destructor)instancemethod_dealloc, /*tp_dealloc*/
  1443.     0,            /*tp_print*/
  1444.     0,            /*tp_getattr*/
  1445.     0,            /*tp_setattr*/
  1446.     (cmpfunc)instancemethod_compare, /*tp_compare*/
  1447.     (reprfunc)instancemethod_repr, /*tp_repr*/
  1448.     0,            /*tp_as_number*/
  1449.     0,            /*tp_as_sequence*/
  1450.     0,            /*tp_as_mapping*/
  1451.     (hashfunc)instancemethod_hash, /*tp_hash*/
  1452.     0,            /*tp_call*/
  1453.     0,            /*tp_str*/
  1454.     (getattrofunc)instancemethod_getattr, /*tp_getattro*/
  1455.     0,            /*tp_setattro*/
  1456. };
  1457.